/*
 * Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
 *
 * Licensed under the Mulan PSL v2.
 * You can use this file according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of the License at
 *
 *     http://license.coscl.org.cn/MulanPSL2
 *
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
 * FOR MORE DETAILS.
 */

#pragma once

#include "highfive/H5File.hpp"

#include "DBManager/IRepository/Input/Design/IDRESP1Repository.h"
#include "DBManager/Repository/HDF5/Base/MainlyRepositoryBase.h"
SG_DBMANAGER_HDF5_NAMESPACE_OPEN
namespace DRESP1
{
    struct IDENTITY
    {
        SG::DataStructure::Common::Id   ID        = 0;      ///<
        char                            LABEL[8]  = { 0 };  ///<
        char                            RTYPE[8]  = { 0 };  ///<
        char                            PTYPE[8]  = { 0 };  ///<
        SG::DataStructure::Common::Id   RPSID     = 0;      ///<
        char                            NTUSED[4] = { 0 };  ///<
        SG::DataStructure::Common::Id   AFPMID    = 0;      ///<
        SG::DataStructure::Common::I64  REGION    = 0;      ///<
        SG::DataStructure::Common::I64  ATTA      = 0;      ///<
        SG::DataStructure::Common::I64  ATTBI     = 0;      ///<
        SG::DataStructure::Common::Real ATTBR     = 0.0;    ///<
        SG::DataStructure::Common::I64  ATTI_LEN  = 0;      ///<
        SG::DataStructure::Common::I64  ATTI_POS  = 0;      ///<
        SG::DataStructure::Common::Id   DOMAIN_ID = 0;      ///<
    };
};  // namespace DRESP1
SG_DBMANAGER_HDF5_NAMESPACE_CLOSE
template <>
inline HighFive::DataType HighFive::create_datatype<SG::DBManager::HDF5::DRESP1::IDENTITY> ()
{
    HighFive::CompoundType compoundType = { { "ID", HighFive::create_datatype<SG::DataStructure::Common::Id> () },
                                            { "LABEL", HighFive::create_datatype<char[8]> (), 1, true },
                                            { "RTYPE", HighFive::create_datatype<char[8]> (), 1, true },
                                            { "PTYPE", HighFive::create_datatype<char[8]> (), 1, true },
                                            { "RPSID", HighFive::create_datatype<SG::DataStructure::Common::Id> () },
                                            { "NTUSED", HighFive::create_datatype<char[4]> (), 1, true },
                                            { "AFPMID", HighFive::create_datatype<SG::DataStructure::Common::Id> () },
                                            { "REGION", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
                                            { "ATTA", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
                                            { "ATTBI", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
                                            { "ATTBR", HighFive::create_datatype<SG::DataStructure::Common::Real> () },
                                            { "ATTI_LEN", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
                                            { "ATTI_POS", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
                                            { "DOMAIN_ID", HighFive::create_datatype<SG::DataStructure::Common::Id> () } };
    return compoundType;
}

template <>
inline HighFive::DataType HighFive::create_datatype<SG::DataStructure::FEM::DRESP1Data::ATTIData> ()
{
    HighFive::CompoundType ct = { { "ATTI", HighFive::create_datatype<SG::DataStructure::Common::I64> () } };
    return ct;
}
SG_DBMANAGER_HDF5_NAMESPACE_OPEN
class DRESP1Repository : public IDRESP1Repository, public SG::DBManager::HDF5::MainlyRepositoryBase<SG::DataStructure::FEM::DRESP1Data>
{
    using MainStruct    = SG::DataStructure::FEM::DRESP1Data;
    using SubStructATTI = SG::DataStructure::FEM::DRESP1Data::ATTIData;

    using NasMainStruct = SG::DBManager::HDF5::DRESP1::IDENTITY;

  public:
    explicit DRESP1Repository (HDF5Database& database);

    enum DATASET : std::uint8_t
    {
        MainDataset,
        IndexDataset,
        SubDataset_ATTI
    };

    /**
     * @brief   : 初始化表数据
     * @author  : wuxingchuang
     */
    void InitialDatasetInfo () override;

    /**
     * @brief   : 将缓存数据转储到文件内
     */
    void DumpWriteCahce () override;

    /**
     * @brief   : 加载文件数据至缓存
     * @param   : loadIndex
     */
    std::vector<DataType>* LoadReadCache (std::size_t loadIndex) override;

    /**
     * @brief   : 对数据进行排序
     */
    void SortData () override;

    /**
     * @brief   : 获取所有数据
     */
    std::vector<MainStruct> GetAllData () override;

    /**
     * @brief   : 将自己的数据转存为nastran的数据结构
     */
    void OwnToNastran (std::vector<MainStruct>&         own,
                       std::vector<NasMainStruct>& _OUT IDENTITYData,
                       std::vector<SubStructATTI>& _OUT subDataATTI,

                       bool isSort = false);

    /**
     * @brief   : 将NASTRAN的数据转存为自己的数据结构
     * @param   : IDENTITYData
     * @param   : subData
     * @param   : own
     */
    void
    NastranToOwn (std::vector<NasMainStruct>& IDENTITYData, std::vector<SubStructATTI>& subDataATTI, std::vector<MainStruct>& _OUT own);
};
SG_DBMANAGER_HDF5_NAMESPACE_CLOSE
